package net.xujialiang.XTestRPA.Controller.TenantManager;

import net.xujialiang.XTestRPA.Config.SpringSecurityConfig.SecurityUser;
import net.xujialiang.XTestRPA.Model.ResultData;
import net.xujialiang.XTestRPA.Service.Tenant.XRPATenantService;
import net.xujialiang.XTestRPA.Service.XRPAUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static net.xujialiang.XTestRPA.Enum.ReturnCode.RC999;

@RequestMapping("/api/tenant/user")
@RestController
public class TenantUserMgr {
    @Autowired
    XRPAUserService userService;
    @Autowired
    XRPATenantService tenantService;

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultData create(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        String userName = (String) reqBody.get("username");
        String nickName = (String) reqBody.getOrDefault("nickname", "");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            // 判断是否是租户管理员
            if(tenantService.isTenantAdmin(ttc, sUser.getUsername())){
                if(userService.checkLimit(ttc)){
                    if(userService.queryUserByNameAndTTC(userName, ttc)==null){
                        userService.createuser(userName,"3.1415926", ttc, nickName, true);
                        return ResultData.success();
                    }else{
                        throw new Exception("用户已存在");
                    }
                }else{
                    throw new Exception("超过用户数数限制");
                }
            }else{
                throw new Exception("非租户管理员, 无权操作");
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/delUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultData delUser(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        String userName = (String) reqBody.get("username");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            // 判断是否是租户管理员
            if(tenantService.isTenantAdmin(ttc, sUser.getUsername())){
                userService.deluser(userName, ttc);
                return ResultData.success();
            }else{
                throw new Exception("非租户管理员, 无权操作");
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultData updateUser(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        String userName = (String) reqBody.get("username");
        String nickName = (String) reqBody.getOrDefault("nickname", "");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        try {
            // 判断是否是租户管理员
            if(tenantService.isTenantAdmin(ttc, sUser.getUsername())){
                userService.updateNickName(userName, ttc, nickName);
                return ResultData.success();
            }else{
                throw new Exception("非租户管理员, 无权操作");
            }
        }catch (Exception e){
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/fetchUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultData fetchUser(@RequestBody Map<String, Object> reqBody, HttpServletRequest request, Authentication auth) {
        Integer page = (Integer) reqBody.get("page");
        Integer pageSize = (Integer) reqBody.get("pageSize");
        SecurityUser sUser = (SecurityUser) auth.getPrincipal();
        String ttc = sUser.getTTC();
        String account = (String) reqBody.get("account");
        String nickname = (String) reqBody.get("nickname");
        try {
            Map<String, Object> ret = userService.fetchUserByTTCAndPage(ttc, account, nickname, page, pageSize);
            return ResultData.success(ret);
        } catch (Exception e) {
            return ResultData.fail(RC999.getCode(), e.getMessage());
        }
    }
}
